/**
 * Created with JetBrains WebStorm.
 * User: Administrator
 * Date: 15-11-6
 * Time: 下午3:57
 * To change this template use File | Settings | File Templates.
 */
function pi(){
    return  3.1415926535898;
}
pow=Math.pow;
sin=Math.sin;
cos=Math.cos;
asin=Math.asin;
min=Math.min;
sqrt=Math.sqrt;
window.onload=function(){
    var url = getUrl();
    var userId = getUserId();
    var loc = getLoc();
    $.jsonp({
        url: 'http://'+url+'/api/user/charisma',
        data: {userId:userId},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            $.each(data.arrays,function(index,item) {
                earthRadius = 6371;
                var lat0 = (loc.lat * pi()) / 180;
                var lng0 = (loc.lng * pi()) / 180;
                var lat1 = (item.userDimensions * pi()) / 180;
                var lng1 = (item.userLongitude * pi()) / 180;
                calcLongitude = lng1 - lng0;
                calcLatitude = lat1 - lat0;
                stepOne = pow(sin(calcLatitude / 2), 2) + cos(lat0) * cos(lat1) * pow(sin(calcLongitude / 2), 2);
                stepTwo = 2 * asin(min(1, sqrt(stepOne)));
                calculatedDistance = earthRadius * stepTwo;
                item.distance=calculatedDistance.toFixed(1);
                (function (){
                    var n = new Date().getTime();
                    o = n-item.timeGap * 1000;
                    item.timeGap = ago(o);//返回距今时间
                    //传入时间毫秒 如果是时间戳 *1000
                })();
            });
            var complied_tpl = juicer($("#mx_tpl").text());
            var html = complied_tpl.render(data);
            $(".order-list").append(html);
        },
        error: function (xOptions, textStatus) {}
    });
}
function onBackKeyDown() {
    jumpbyback(2,"");
}

function ago(o){
    var times =o;
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var month = day * 30;
    var year = month * 365;
    var yearC = times/year;
    var monthC =times/month;
    var dayC =times/day;
    var hourC =times/hour;
    var minC =times/minute;
    var html ="";
    if(yearC>=1){
        html += parseInt(yearC) + "年前";
        return html;
    }else if(monthC>=1){
        html += parseInt(monthC) + "月前";
        return html;
    }else if(dayC>=1){
        html += parseInt(dayC) + "天前";
        return html;
    }else if(hourC>=1){
        html += parseInt(hourC) + "个小时前";
        return html;
    }else if(minC>=1){
        html += parseInt(minC) + "分钟前";
        return html;
    }else{
        return "刚刚";
    }
}